package com.wlr.blog.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wlr.blog.util.common.dto.Result;
import com.wlr.blog.web.common.entity.Admin;
import com.wlr.blog.web.common.service.AdminService;
import com.wlr.blog.web.common.vo.AdminVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lrwang
 * @version 1.0
 * @date 2022/11/12 15:47
 */
@RestController
@Slf4j
public class AdminController {
    @Autowired
    AdminService adminService;

    @GetMapping("/admin_info")
    public Result<?> getInfo() {
        Admin byId = adminService.getById(1);
        return Result.success(byId);
    }

    @PostMapping("/login")
    public Result login(@RequestBody Admin admin) {
        boolean login = adminService.login(admin);
        if (login) {
            AdminVo adminVo = new AdminVo();
            BeanUtils.copyProperties(adminService.getOne(new QueryWrapper<Admin>()
                    .eq("user_name", admin.getUserName())), adminVo);
            adminVo.setToken("tokenTest");
            return Result.success("登录成功", adminVo);
        }
        return Result.error("登录失败");
    }
}
